<?php
/**
 * Ora 系统缓存类
 *
 */
class cache extends base {

	/**
	 * 判断执行ID的缓存是否可用
	 *
	 * @param string $cache_id
	 * @return boolean
	 */
	public static function isCached($type='tpl',$cache_id){
		$cache_id = sprintf('%X', crc32($cache_id));
		return !io::isExp(CACHE_PATH."{$type}_data/{$cache_id}.php",$GLOBALS['ora_cfg']['data_cache']);
	}

	/**
	 * 写入缓存
	 *
	 * @param unknown_type $type
	 * @param unknown_type $id
	 * @param unknown_type $val
	 */
	public static function  write($type='tpl',$cache_id,$val){
		$cache_id = sprintf('%X', crc32($cache_id));
		$f = CACHE_PATH."{$type}_data/{$cache_id}.php";
		$str = "<?php exit(); ?>".serialize($val);
		io::write($f,$str);
	}

	/**
	 * 读取缓存
	 *
	 * @param unknown_type $type
	 * @param unknown_type $id
	 * @return unknown
	 */
	public static function read($type='tpl',$cache_id){
		$cache_id = sprintf('%X', crc32($cache_id));
		$f = CACHE_PATH."{$type}_data/{$cache_id}.php";
		if(is_file($f)){
			return unserialize(preg_replace('/<\?php(.*?)\?>/i',"",file_get_contents($f)));
		}
		return false;
	}

}
?>